/**
 * 
 */
package com.ofek.eventos.db;

import java.util.ArrayList;
import java.util.Iterator;

import com.ofek.base.db.ComplexDataField;
import com.ofek.base.db.DataField;
import com.ofek.base.db.DataTypeClass;
import com.ofek.db.Table;
import com.ofek.eventos.net.lks.IMap;
import com.ofek.eventos.net.lks.ItemVersion;
import com.ofek.eventos.net.lks.test.Request;
import com.ofek.eventos.net.lks.test.RequestID;

/**
 * @author Ofek
 *
 */
public class RequestsTable implements IMap<RequestID, ItemVersion<RequestID,Request>> {

	@DataTypeClass
	public static class RequestVersion {
		@ComplexDataField
		RequestID id;
		@ComplexDataField
		Request item;
		@DataField
		long version;
		public ItemVersion<RequestID, Request> toItemVersion() {
			return new ItemVersion<>(id, item, version);
		}
		public RequestVersion(final ItemVersion<RequestID, Request> iv) {
			id = iv.id;
			item = iv.item;
			version = iv.version;
		}
	}

	private final Table<RequestVersion> table;
	/**
	 * @param name 
	 * 
	 */
	public RequestsTable(final String name) {
		table = new Table<RequestVersion>(name,RequestVersion.class);
	}


	@Override
	public Iterable<ItemVersion<RequestID, Request>> values() {
		final ArrayList<RequestVersion> all = table.getAll();
		return new Iterable<ItemVersion<RequestID,Request>>() {

			@Override
			public Iterator<ItemVersion<RequestID, Request>> iterator() {
				return new Iterator<ItemVersion<RequestID,Request>>() {
					Iterator<RequestVersion> it = all.iterator();
					@Override
					public void remove() {
						throw new UnsupportedOperationException();
					}

					@Override
					public ItemVersion<RequestID, Request> next() {
						return it.next().toItemVersion();
					}

					@Override
					public boolean hasNext() {
						return it.hasNext();
					}
				};
			}
		};
	}
	/* (non-Javadoc)
	 * @see com.ofek.eventos.net.lks.IMap#remove(java.lang.Object)
	 */
	@Override
	public ItemVersion<RequestID, Request> remove(final RequestID key) {
		final RequestVersion eventVersion = table.get("id_src="+key.src+" and id_dest="+key.dest+" and id_id="+key.id);
		if ( eventVersion!=null ) {
			table.remove("id_src="+key.src+" and id_dest="+key.dest+" and id_id="+key.id);
			return eventVersion.toItemVersion();
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see com.ofek.eventos.net.lks.IMap#get(java.lang.Object)
	 */
	@Override
	public ItemVersion<RequestID, Request> get(final RequestID key) {
		return table.get("id_src="+key.src+" and id_dest="+key.dest+" and id_id="+key.id).toItemVersion();
	}

	/* (non-Javadoc)
	 * @see com.ofek.eventos.db.EventosDatabase.VersionTable#put(java.lang.Object, com.ofek.eventos.net.lks.ItemVersion)
	 */
	@Override
	public ItemVersion<RequestID, Request> put(final RequestID key,
			final ItemVersion<RequestID, Request> newValue) {
		final ArrayList<RequestVersion> query = table.query("SELECT * FROM "+table.getTableName() + " where id_src="+key.src+" and id_dest="+key.dest+"and id_id="+key.id+" ORDER BY version DESC LIMIT 1;");
		table.add(new RequestVersion(newValue));
		return query.size()>0 ? query.get(0).toItemVersion() : null;
	}
}

